import cocos

class DialogLayer(cocos.layer.ColorLayer):
    is_event_handler = True
    def __init__(self, persistent=False):
        super(DialogLayer, self).__init__(255, 255, 255, 100)
        self.persistent = persistent

    def on_enter(self):
        super(DialogLayer, self).on_enter()
        self.setup()

    def on_exit(self):
        super(DialogLayer, self).on_exit()
        self.cleanup()

    def setup(self):
        raise NotImplementedError('implement setup')

    def cleanup(self):
        pass

    def close(self, *args):
        if self.persistent:
            return
        self.parent.remove(self)
        return True

    def squash(self, *args): return True

    # close on text or mouse release
    on_text = squash
    on_text_motion = squash
    on_text_motion_select = squash
    on_key_press = close
    on_key_release = squash
    on_mouse_release = close
    on_mouse_press = squash
    on_mouse_motion = squash
    on_mouse_drag = squash

